<?php
/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 2020/12/16
 * Time: 16:28
 */

namespace app\admin\controller;


use app\common\model\GoodsModel;
use think\Db;
use think\Validate;

class Goods extends Common
{
    public function lists(){
        $field = 'id,goods_title,goods_images,integral,old_price,sales,stock,create_time';
        $searchKeyWords = input('param.searchKeyWords','');
        $list = Db::name('goods')->whereLike("goods_title","%{$searchKeyWords}%")
            ->where('is_delete',0)
            ->order('id','desc')
            ->field($field)->paginate(15,[]);
        return view('',['list'=>$list]);
    }
    public function add_goods(){
        if(Request()->isPost()) {
            $data = $this->checkData();
            $isHasGoods = Db::name('goods')->where('goods_title',$data['goods_title'])->value('id');
            if($isHasGoods){
                return package(1006,'商品名重复');
            }
            $model = new GoodsModel();
            $data['create_time']  = date("Y-m-d H:i:s");
            $result = $model->allowField(true) ->insert($data);
            return checkRes($result,'添加');
        }else{
            return view();
        }
    }
    public function del($id){
        $result =Db::name('goods')->where('id',$id)->update(['is_delete'=>1]);
        return checkRes($result,'删除');
    }
    public function edit(){
        if(Request()->isPost()) {
            $data = $this->checkData();
            $model = new GoodsModel();
            $data['create_time']  = date("Y-m-d H:i:s");
            $id = $data['id'];
            unset($data['id']);
            $result = $model->allowField(true) ->save($data,['id'=>$id]);
            return checkRes($result,'添加');
        }else{

            $info = Db::name('goods')->where('id',input('param.id/d',0))->find();
            return view('add_goods',['info'=>$info]);
        }
    }

    private function checkData(){
        $param = input('param.');
        $rule = [
            'goods_title|商品名' =>'require|max:30',
            'integral|所需积分' =>'require|float|max:10,2',
           'old_price|原价' =>'require|float|max:10,2',
            'goods_images|商品图片' =>'require|max:240',
           'content|商品内容' =>'require',
           'stock|库存' =>'require|number|max:6',
        ];
        $validate = Validate::make($rule);
        if (!$validate->check($param)) {
            echo json_encode(['code'=>10002,'msg'=>$validate->getError()]);exit;
        }
        return $param;
    }
    public function exchange(){
        $status = input('param.status/d',5);
        $searchKeyWords = input('param.searchWords','');
        $where = [['goods_title|true_name|phone','like',"%{$searchKeyWords}%"]];
        if($status !=5){
            $where[] = ['status','=',$status];
        }
        $field = 'id,headimgurl,goods_title,goods_images,integral,number,true_name,phone,status,create_time';
        $list = Db::name('exchange_order')->where($where)
            ->order('id','desc')
            ->field($field)->paginate(15,[]);
        return view('',['list'=>$list,'info'=>['status'=>$status]]);
    }
    public function info($id){
        $info =  Db::name('exchange_order')->where('id',$id)->find();
        return view('',['info'=>$info]);
    }
    public function fahuo(){
        $id = input('param.id/',0);
        if(!$id){
            return package(100,'id错误');
        }
        $result =  Db::name('exchange_order')->where('id',$id)->update(['status'=>1,'fahuo_time'=>date("Y-m-d H:i:s")]);
        return checkRes($result,'发货');
    }

}